<template>
	<view class="scancode">
	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	import {Base64} from '../../static/base64.min.js';
	export default {
		data() {
			return {
                link : '',
				joinCode: '',
				latitude: '',
				longitude: '',
			}
		},
		methods: {
            confirmCode() {
                serve.request({
                	url: '/site/scancode',
                	data: {
                		appkey: uni.getStorageSync('appkey'),
                		link: this.link,
                	}
                }).then(res => {
                	if (res.code == 200) {
                		if(res.data.type == 1) {
                            uni.redirectTo({
                            	url: res.data.xcxView
                            });
                		    // common.jump(res.data.jumpType, res.data.jumpLink);
                		} else if (res.data.type == 2){
                            this.joinCode = res.data.joinCode;
                		    this.getLocationAddress();
                        } else if (res.data.type == 3){
                            this.enrollFun(res.data.joinCode);
                		}
                	} else {
                		uni.showModal({
                			title: '提示',
                			content: '错误的二维码，请确定后再扫描',
                			showCancel: false,
                			success(res) {
                				common.jump('index');
                			}
                		});
                	}
                });
            },
			getOpenUser(){
				uni.login({
					success: function (res) {
						let wechatCode = res.code;
						serve.request({
							url: '/site/wechat-open',
							data: {
								appkey: uni.getStorageSync('appkey'),
								group: uni.getStorageSync('group'),
								wechatCode: wechatCode,
							}
						})
						.then(res => {
							if (res.code == 200) {
								uni.setStorageSync('access-token', res.data.access_token);
							} else {
								uni.showModal({
									title: '提示',
									content: '未能定位到您的账号，请于小程序内扫码',
									showCancel: false,
									success(res) {
										common.jump('index');
									}
								});
							}
						});
					},
				});
			},
			getLocationAddress(){
				var self = this;
				wx.getLocation({
				    type: 'wgs84',
				    success: (res)=> {
				        self.latitude = res.latitude;
				        self.longitude = res.longitude;
						self.joinFun();
					},
					fail:function(){
						uni.showModal({
							title: '提示',
							content: '请同意小程序获取当前定位',
							showCancel: false,
							success(res) {
								self.joinFun();
							}
						});
					},
				});
			},
			joinFun(){
				var self = this;
				serve.request({
					url: '/member/activity/sign-join',
					data: {
						appkey: uni.getStorageSync('appkey'),
						'access-token': uni.getStorageSync('access-token'),
						'join_code': self.joinCode,
						longitude: self.longitude,
						latitude: self.latitude,
						address: ''
					}
				})
				.then(res => {
					let rr = res;
					if(rr.code == 200) {
						uni.redirectTo({
						    url: '/pages/my/applyDetails?id='+rr.data.itemId
						});
						return ;
					}
					uni.showModal({
						title: '提示',
						content: rr.message,
						showCancel: false,
						success(res) {
							if(rr.data.itemId > 0) {
								uni.redirectTo({
								    url: '/pages/my/applyDetails?id='+rr.data.itemId
								});
								return ;
							}
							common.jump('index');
						}
					});
				});
			},
            enrollFun(paperId) {
            	var _self = this;
            	serve.request({
            		url: '/course/paper/enroll',
            		data: {
            			appkey: uni.getStorageSync('appkey'),
            			'access-token': uni.getStorageSync('access-token'),
            			id: paperId
            		}
            	}).then(res => {
            		if(res.code != 200) {
            			uni.showToast({
            				title: res.message,
            				icon: 'none'
            			});
            			return ;
            		}
            		let jumpType = (res.data.type == 1 ? '/course/examination?id=' : '/course/operation?id=')+res.data.id;
            		let jumpLink = res.data.id;
            		uni.getSetting({
            			withSubscriptions: true,
            			success(res) {
            				if (res.authSetting['scope.subscribeMessage']) { 
            					uni.openSetting({ 
            						success(res) {
            							_self.addTempSetting(jumpLink, '-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU', res['-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU'])
            		                    uni.redirectTo({
            		                        url: jumpType
            		                    });
            						}
            					});
            				} else { 
            					uni.requestSubscribeMessage({
            						tmplIds: ['-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU'],
            						success(res) {
            							_self.addTempSetting(jumpLink, '-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU', res['-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU'])
            		                    uni.redirectTo({
            		                        url: jumpType
            		                    });
            						},
            						fail: (res) => {
            							_self.addTempSetting(jumpLink, '-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU', res['-hy5j4epeZJqAGQJaM13GGcATIMzexucpcBY0y7HZSU'])
            		                    uni.redirectTo({
            		                        url: jumpType
            		                    });
            						}
            					})
            				}
            			}
            		});
            	});
            },
            addTempSetting(sceneId, tempId, status) {
            	if(status == undefined) {
            		return ;
            	}
            	serve.request({
            		url: '/member/main/add-temp-setting',
            		data: {
            			appkey: uni.getStorageSync('appkey'),
            			'access-token': uni.getStorageSync('access-token'),
            			tempType: 'wechat',
            			sceneId: sceneId,
            			tempId: tempId,
            			status: status
            		}
            	}).then(res => {
            		console.info(res);
            	});
            }
		},
		async onLoad(options) {
            // uni.setStorageSync('access-token', '');
            await this.$defaultLogin();
			this.link = options.link || '';
			if(options.q) {
				let q = decodeURIComponent(options.q);
                this.link = Base64.encode(q);
			}
             this.confirmCode();
		},
		// async onShow() {
			
		// 	await this.$defaultLogin();
		// 	// if(uni.getStorageSync('access-token') == "") {
		// 	// 	this.getOpenUser();
		// 	// }
		// 	this.confirmCode();
		// }
	}
</script>

<style  scoped lang="scss">
	.scancode{
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		
		position: fixed;
		top:0;
		left: 0;
		background-color: #ffd900;
		
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		
		
		
		.logos{
			width: 400upx;
			height: 400upx;
			margin-bottom: 80upx;
		}
			
		
		
		.mytxt {
			width: 600upx;
			height: 100upx;
			margin-bottom: 300upx;
		}
		
	}
</style>
